package com.ihr360.sharedservice.model;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class WorkingCalendar {
    private Calendar calendar = Calendar.getInstance();
    Map<Date, StatutoryVacation> holidayCalendar;
    private int legalWorkingDayOfThisMonth;

    public WorkingCalendar( Map<Date, StatutoryVacation> holidayCalendar) {
        this.holidayCalendar = holidayCalendar;
    }


    /**
     * 获取一段时间内除了节假日之外的应工作天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public int getWorkingDaysBetween(Date startDate, Date endDate) {
        calendar.setTime(endDate);
        calendar.add(Calendar.DATE, 1);
        Date endDatePlus1 = calendar.getTime();

        calendar.setTime(startDate);
        Date beginDate = calendar.getTime();
        int workingDays = 0;
        while (beginDate.before(endDatePlus1)) {
            if (isHoliday(beginDate)) {
                beginDate = addOneDate();
                continue;
            }
            if (isSupplementary(beginDate)) {
                beginDate = addOneDate();
                workingDays++;
                continue;
            }
            if (isWeekend(calendar)) {
                beginDate = addOneDate();
                continue;
            }
            beginDate = addOneDate();
            workingDays++;
        }
        return workingDays;
    }

    /**
     * 获取一段时间法定计薪天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public int getLegalPaidDayOfThisMonth(Date startDate, Date endDate) {
        calendar.setTime(endDate);
        calendar.add(Calendar.DATE, 1);
        Date endDatePlus1 = calendar.getTime();

        calendar.setTime(startDate);
        Date beginDate = calendar.getTime();
        int workingDays = 0;
        while (beginDate.before(endDatePlus1)) {
            if (isHoliday(beginDate)) {
                beginDate = addOneDate();
                workingDays++;
                continue;
            }
            if (isSupplementary(beginDate)) {
                beginDate = addOneDate();
                workingDays++;
                continue;
            }
            if (isWeekend(calendar)) {
                beginDate = addOneDate();
                continue;
            }
            beginDate = addOneDate();
            workingDays++;
        }
        return workingDays;
    }

    private Date addOneDate() {
        Date beginDate;
        calendar.add(Calendar.DATE, 1);
        beginDate = calendar.getTime();
        return beginDate;
    }

    /**
     * 是否是被调整的
     *
     * @param date
     * @return
     */
    public boolean isSupplementary(Date date) {
        StatutoryVacation vacation = holidayCalendar.get(date);
        if (vacation == null) {
            return false;
        }
        if (VacationCategory.SUPPLEMENTARY.equals(vacation.getVacationCategory())) {
            return true;
        }
        return false;
    }

    /**
     * 是否是假期
     *
     * @param date
     * @return
     */
    public boolean isHoliday(Date date) {
        StatutoryVacation vacation = holidayCalendar.get(date);
        if (vacation == null) {
            return false;
        }
        if (VacationCategory.STATUTORY.equals(vacation.getVacationCategory())) {
            return true;
        }
        return false;
    }

    /**
     * 是否是法定节假日
     *
     * @param date
     * @return
     */
    public boolean isStatutoryHoliday(Date date) {
        StatutoryVacation vacation = holidayCalendar.get(date);
        if (vacation == null) {
            return false;
        }
        if (Boolean.TRUE.equals(vacation.getIsStatutoryHoliday())) {
            return true;
        }
        return false;
    }

    public boolean isWeekend(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        }
        return false;
    }

    /**
     * 是否是周末
     *
     * @param calendar
     * @return
     */
    private boolean isWeekend(Calendar calendar) {
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        }
        return false;
    }

}